﻿<script type="text/javascript">
    var deviceid = 0;
    function onDeviceAction(e) {

        if (e.id != "btnAddDevice" && deviceid == 0) {
            alert("Please select a device first");
            return;
        }

        if (e.id == "btnAddDevice") {

            $.post("Device/AddDevice",
                    "deviceSerialNo=" + $("#deviceSerialNoTxt")[0].value,
                    "json")
            .success(
                function (data) {
                    if (data.toLowerCase().indexOf("succeed") > 0) {
                        var deviceGrid = $('#DeviceGrid').data('tGrid');
                        deviceGrid.ajaxRequest();
                    } else {
                        ReplaceContentInContainer("result", data);
                    };
                })
            .error(function () { alert("error"); })
                            

        }else if (e.id == "btnSaleDevice") {
            var customer = $("#Customers").data("tDropDownList");

            $.post("Device/SellDevice",
                    "customerid=" + customer.value() + "&deviceid=" + deviceid,
                    "json")
            .success(
                function (data) {
                    if (data.toLowerCase().indexOf("succeed") > 0) {
                        var deviceGrid = $('#DeviceGrid').data('tGrid');
                        deviceGrid.ajaxRequest();
                        customer.reload();
                    } else {
                        ReplaceContentInContainer("result", data);
                    };
                })
            .error(function () { alert("error"); })
       }
        //            var deviceGrid = $('#DeviceGrid').data('tGrid');
        //            idcID = e.row.cells[1].innerHTML;
        //            $('#idcID').text(idcID);
        //            deviceGrid.rebind();
    }
      function onRowSelected(e) {
          deviceid = e.row.cells[0].innerHTML;
          ReplaceContentInContainer("result","Selected DeviceID=" + deviceid );
        }
   

    function ReplaceContentInContainer(id,content) {
        var container = document.getElementById(id);
        container.innerHTML = content;
    }

</script>    
